package com.yoomiito.app.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import com.yoomiito.app.utils.DensityUtil

class TriangleView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0)
    :View(context, attrs, defStyleAttr, defStyleRes){
    private var mWidth = 0
    private var mHeight = 0

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //width
        // 获取测量模式（Mode）
        val specMode = MeasureSpec.getMode(widthMeasureSpec)
        // 获取测量大小（Size）
        val specSize = MeasureSpec.getSize(widthMeasureSpec)
        mWidth = when (specMode) {
            MeasureSpec.AT_MOST -> DensityUtil.dp2px(16f)
            MeasureSpec.EXACTLY -> specSize
            else -> specSize
        }

        //width
        // 获取测量模式（Mode）
        val specModeH = MeasureSpec.getMode(heightMeasureSpec)
        // 获取测量大小（Size）
        val specSizeH = MeasureSpec.getSize(heightMeasureSpec)
        mHeight = when (specModeH) {
            MeasureSpec.AT_MOST -> DensityUtil.dp2px(16f)
            MeasureSpec.EXACTLY -> specSizeH
            else -> specSizeH
        }
        setMeasuredDimension(mWidth,mHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        val p =  Paint()
        p.color = Color.parseColor("#FF035B")
        //实例化路径
        val path =  Path()
        path.moveTo(0f, mHeight.toFloat())// 此点为多边形的起点
        path.lineTo(mWidth.toFloat(), mHeight.toFloat())
        path.lineTo(mWidth.toFloat()/2, 0f)
        path.close() // 使这些点构成封闭的多边形
        canvas?.drawPath(path, p)
    }
}